package sliver.action.shop;

import java.io.Serializable;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Shopper;
import sliver.service.ShopManager;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.addCategoryAction")
public class AddCategoryAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = -5444746721338505874L;
	
	private String name;
	private Long categoryParentId;
	
	private Map<String, Object> session;
	
	@Autowired
	private ShopManager shopManager;
	
	public AddCategoryAction(){
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getCategoryParentId() {
		return categoryParentId;
	}

	public void setCategoryParentId(Long categoryParentId) {
		this.categoryParentId = categoryParentId;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		
		Shopper shopper = Shopper.class.cast(session.get("login_shopper"));
		
		Serializable id = shopManager.saveCategory(shopper, name, categoryParentId);
		if(id != null){
			return SUCCESS;
		}
		
		return ERROR;
	}
}
